package com.web;

import com.domain.User;
import com.service.userService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 客户自己修改个人数据的数据作用，现在查询员工个人数据
 * 接收 userSelfAct.jsp 页面的 phone、 password数据
 * 提交到 userService 的 findUser 函数
 * 转发到 /updateUserSelf.jsp 页面进行展示
 */
@WebServlet("/findUserSelf")
public class findUserSelfServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        //1.接受有传入的参数
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");

        //2.调用service层的,根据phone,password查询
        User user = new userService().findUser(phone,password);
        System.out.println("查询了 user = " + user +" 的用户");
        //3.判断对象存不存在
        if(user != null){
            //4.1对象存在展示
            request.setAttribute("user", user);
            //4.2转发到update.jsp进行展示
            request.getRequestDispatcher("/updateUserSelf.jsp").forward(request,response);
        }else{
            //5.对象不存在,重定向到查询所有
            response.sendRedirect(request.getContextPath()+"/ShowUserSelf?userId="+user.getUserId());
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
